package com.itheima.controller;

import com.itheima.entity.Page;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.BlogSort;
import com.itheima.service.BlogSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Description:
 *
 * @author Wangchang
 * @date 2020/8/3 19:57
 */
@RestController
@RequestMapping("/blogSort")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST}, origins = "*")
public class BlogSortController {
    @Autowired
    private BlogSortService blogSortService;

    @PostMapping("/add")
    public Result add(@RequestBody BlogSort tBlogSort) {

        try {
            blogSortService.add(tBlogSort);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR + "", "该实体已经存在");
        }
        return new Result(StatusCode.OK + "", "添加成功");
    }

    @PostMapping("/blogSortByCite")
    public Result blogSortByCite() {


        return new Result(StatusCode.OK + "", "操作成功");
    }

    @PostMapping("/blogSortByClickCount")
    public Result blogSortByClickCount() {
        blogSortService.blogSortByClickCount();

        return new Result(StatusCode.OK + "", "操作成功");
    }

    
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<BlogSort> list) {
        try {
            blogSortService.delet(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR + "", "操作失败");
        }
        return new Result(StatusCode.OK + "", "操作成功");
    }

    @PostMapping("/getList")
    public Result getList(@RequestBody Map map) {

        List<BlogSort> list = blogSortService.selectSort();
        Page page = new Page();
        page.setRecords(list);
        return new Result("success", page);

    }

}
